<?php

namespace app\common\model\product;

use app\api\model\supplier\Supplier as SupplierModel;
use app\common\exception\BaseException;
use app\common\library\helper;
use app\common\model\BaseModel;
use think\facade\Db;

/**
 * 商品模型
 */
class ProductFormwork extends BaseModel
{
    protected $name = 'product_formwork';
    protected $pk = 'formwork_id';

    public function product()
    {
        return $this->hasOne(Product::class, 'product_id', 'product_id');
    }

    /**
     * 关联商品分类表
     */
    public function category()
    {
        return $this->belongsTo('app\\common\\model\\product\\Category');
    }

    // 详情
    public static function detail($formwork_id)
    {
        return self::withoutGlobalScope()->find($formwork_id);
    }

    /**
     * 隐藏字段
     */
    public static function listData($where, $params, $with = [], $sort = '',$join=[])
    {
        $where = self::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        if(count($join)>0){
            $model = $model->alias('a')->join($join[0],$join[1]);
        }
        if (!empty($sort)) {
            $model = $model->order($sort);
        } else {
            $model = $model->order(self::baseOrder());
        }
        $model = self::assWhere($model, $where);
        $list = $model->paginate($params);
        return $list;
    }

    public static function createData($where)
    {
        $model = new static();
        $where['app_id'] = self::$app_id;
        $data = $model->save($where);
        return $data;
    }

    public static function one($where, $with = [])
    {
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        return $model->findOrEmpty($where);
    }

    public static function deleteData($where)
    {
        $model = new static();
        return $model->where(['id' => $where['id']])->delete();
    }

    public static function listAll($where = [], $with = [], $sort = '')
    {
        $where = self::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        $model = self::assWhere($model, $where);
        if (!empty($sort)) {
            $model = $model->order($sort);
        } else {
            $model = $model->order(self::baseOrder());
        }
        $list = $model->select();
        return $list;
    }

    public static function assWhere($model, $where)
    {
        if (array_key_exists('name', $where) && $where['name'] != '') {
            $model = $model->where('name', 'LIKE', '%' . $where['name'] . '%');
            unset($where['name']);
        } else {
            unset($where['name']);
        }
        if (array_key_exists('title', $where) && $where['title'] != '') {
            $model = $model->where('title', 'LIKE', '%' . $where['title'] . '%');
            unset($where['title']);
        } else {
            unset($where['title']);
        }
        if (array_key_exists('create_time', $where) && $where['create_time'][0] != '') {
            $model = $model->where('create_time', '>=', strtotime($where['create_time'][0]));
            $model = $model->where('create_time', '<=', strtotime($where['create_time'][1]));
            unset($where['create_time']);
        } else {
            unset($where['create_time']);
        }

        if (isset($where['category_id']) && $where['category_id'] > 0 ) {
            $model = $model->where('category_id', 'IN', $where['category_id']);
            unset($where['category_id']);
        }else{
            unset($where['category_id']);
        }


        foreach ($where as $k => $item) {
            if ($item == '') {
                unset($where[$k]);
            }
            if ($k == 'is_show') {
                $where['a.is_show'] = $item;
                unset($where[$k]);
            }
        }

        return $model->where($where);
    }

}
